<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/2/29
 * Time: 15:51
 */

namespace app\common\model\mysql;


use think\Model;

class ConfigGroup extends Model
{
    protected $autoWriteTimestamp = true;

    protected $hidden = ['update_time'];

    public function getCreateTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * @desc 添加配置组
     * @param $data
     * @return bool
     */
    public function add($data)
    {
        if (empty($data) || !is_array($data)) {
            return false;
        }

        return $this->save($data);
    }

    /**
     * @desc 分页查询所有配置组信息
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function listOfConfigGroup()
    {
        return $this->paginate(10);
    }

    /**
     * @desc 根据id查询配置组信息
     * @param $id
     * @return array|bool|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getConfigGroupById($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return false;
        }

        $where = ['id' => $id];

        $result = $this->where($where)->find();

        return $result;
    }

    public function updateById($id, $data)
    {
        $id = intval($id);
        if (empty($id) || empty($data) || !is_array($data)) {
            return false;
        }

        $where = [
            'id' => $id,
        ];

        return $this->where($where)->save($data);
    }

    public function deleteById($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return false;
        }
        $where = [
            'id' => $id,
        ];

        return $this->where($where)->delete();
    }

    public function getAllGroups()
    {
        return $this->field('id,group_name,group_title')->select();
    }

    public function configs()
    {
        return $this->hasMany('Config', 'group_id', 'id');
    }
}